const router = require('koa-router')()
const { SuccessModel, ErrorModel } = require('../model/resModel')
const {
  getMessages,
  getComments,
  createMessage,
  createComment,
  handleLike
} = require('../controller/message')
const checkToken = require('../middleware/checkToken')

router.get('/message/list', async (ctx, next) => {
  const result = await getMessages()
  ctx.body = new SuccessModel(result)
})

router.get('/comment/list', async (ctx, next) => {
  const query = ctx.query
  const message_id = query.message_id
  const result = await getComments(message_id)
  ctx.body = new SuccessModel(result)
})

router.post('/message/new', checkToken, async (ctx, next) => {
  const postData = ctx.request.body
  const result = await createMessage(postData)
  ctx.body = new SuccessModel(result, '新建留言成功🎉')
})

router.post('/comment/new', checkToken, async (ctx, next) => {
  const postData = ctx.request.body
  const result = await createComment(postData)
  ctx.body = new SuccessModel(result, '新建评论成功🎉')
})

router.post('/message/like', checkToken, async (ctx, next) => {
  const postData = ctx.request.body
  await handleLike(postData)
  ctx.body = new SuccessModel('操作成功')
})

module.exports = router
